package com.hnx.admin.system.controller;

import cn.hutool.core.util.IdUtil;
import com.hnx.admin.api.http.HttpStatusAPI;
import com.hnx.admin.common.captcha.config.CaptchaConfig;
import com.hnx.admin.common.security.core.SecurityProperties;
import com.hnx.admin.system.servce.SysConfigService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/*
创建人： 倔强的头发
创建时间：2024/7/10
功能描述:

*/
@RestController
@RequestMapping("sys")
@RequiredArgsConstructor
public class SysController implements HttpStatusAPI {
    private final CaptchaConfig captcherConfig;
    private final SecurityProperties securityProperties;
    private final SysConfigService configService;
    @GetMapping("/captchaImage")
    public Map<String, Object> getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uuid_req = securityProperties.getUuid();
        String req = request.getParameter(uuid_req);
        Map<String,Object> json = new HashMap<>();
        json.put(CODE_TAG,DEFAULT_HTTP_SUCCESS_CODE);
        json.put(MSG_TAG,DEFAULT_HTTP_SUCCESS_MSG);
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        json.put("captchaEnabled", captchaEnabled);
        String uuid = Optional.ofNullable(req).orElse(IdUtil.simpleUUID());
        json.put(uuid_req, uuid);
        if (!captchaEnabled)
        {
            return json;
        }
        return captcherConfig.createCode(uuid, response,json);
    }
}
